/*
 * @Description: 
 * @Author: changqing
 * @Date: 2021-08-16 14:51:18
 * @LastEditTime: 2021-08-16 14:51:52
 * @LastEditors: changqing
 * @Usage: 
 */
class Producer {
	constructor() {
		this.observers = [];
	}
	addListener(observer) {
		this.observers.push(observer)
	}
	removeListener(listener) {
		this.observers.splice(this.listeners.indexOf(observer), 1)
	}
	notify(message) {
		this.observers.forEach(observer => {
			observer.update(message);
		})
	}
}

const subject = new Producer()
class Observer {
	constructor(name) {
		this.name = name
	}
	update(message){
		console.log(message + ' ' +this.name)
	}
}
subject.addListener(new Observer('ZF')); // 注册监听
subject.addListener(new Observer('practice'));

subject.notify('A new observer') // 执行